﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CXJ.Libiary
{
    public class DataFactory
    {
        public static Interface.IDataOperater Create(Type t)
        {
            Interface.IDataOperater tempDataOperater = null;

            try
            {
                string entityName = t.Name;

                Interface.IBaseEntity entity = CreateEntity(t);

                switch (entityName)
                {
                    default:
                        tempDataOperater = (Interface.IDataOperater)System.Reflection.Assembly.Load(DBManager._DBTYPE).CreateInstance(DBManager._DBOPERATENAME);
                        tempDataOperater.BaseEntity = entity;
                        break;
                }
            }
            catch 
            {
            }

            return tempDataOperater;
        }

        public static Interface.IBaseEntity CreateEntity(Type t)
        {
            Interface.IBaseEntity baseEntity = null;
            object objs = Libiary.CacheManger.Instance.Get(t.FullName);
            if (objs != null && objs is Interface.IBaseEntity)
            {
                baseEntity = objs as Interface.IBaseEntity;
            }
            else
            {
                baseEntity = (Interface.IBaseEntity)System.Activator.CreateInstance(t);
                Libiary.CacheManger.Instance.Add(t.FullName, baseEntity);
            }
            return baseEntity;
        }
    }
}
